﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace smarx.HostedWebCore
{
    public static class NativeMethods
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    }

    public static class HWCPInterop
    {
        public delegate int FnWebCoreActivate([In, MarshalAs(UnmanagedType.LPWStr)]string appHostConfig, [In, MarshalAs(UnmanagedType.LPWStr)]string rootWebConfig, [In, MarshalAs(UnmanagedType.LPWStr)]string instanceName);
        public delegate int FnWebCoreShutdown(bool immediate);

        public static FnWebCoreActivate WebCoreActivate;
        public static FnWebCoreShutdown WebCoreShutdown;

        static HWCPInterop()
        {
            // Load the library and get the function pointers for the WebCore entry points
            const string HWCPath = @"%windir%\system32\inetsrv\hwebcore.dll";
            IntPtr hwc = NativeMethods.LoadLibrary(Environment.ExpandEnvironmentVariables(HWCPath));

            IntPtr procaddr = NativeMethods.GetProcAddress(hwc, "WebCoreActivate");
            WebCoreActivate = (FnWebCoreActivate)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(FnWebCoreActivate));

            procaddr = NativeMethods.GetProcAddress(hwc, "WebCoreShutdown");
            WebCoreShutdown = (FnWebCoreShutdown)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(FnWebCoreShutdown));
        }
    }
}